package com.example.springmvcdemo2;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/request")
public class UserController {
    @RequestMapping("/r1")
    public String r1(String username) {
        return "name: " + username;
    }

    @RequestMapping("/r2")
    public String r2(String name, Integer age) {
        return "name: " + name + " age: " + age;
    }

    @RequestMapping("/r3")
    public String r3(UserInfo user) {
        return user.toString();
    }

    @RequestMapping("/r4")
    public String r4(@RequestParam("name") String username, @RequestParam(value = "age", required = false) Integer userage) {
        return "username: " + username + " userage: " + userage;
    }

    @RequestMapping("/r5")
    public String r5(String[] arr) {
        return Arrays.toString(arr);
    }

    @RequestMapping("/r6")
    public String r6(@RequestParam("list") List<String> list) {
        return "size: " + list.size() + " list: " + list;
    }

    @RequestMapping("/r7")
    public String r7(@RequestBody UserInfo user) {
        return user.toString();
    }

    @RequestMapping({"/r8/{name}/{age}", "r8/{age}"})
    public String r8(@PathVariable(value = "name", required = false) String username, @PathVariable Integer age) {
        return "username: " + username + " age: " + age;
    }

    @RequestMapping("/r9")
    public String r9(@RequestPart MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        file.transferTo(new File("D:/tmp/" + fileName));
        return "接收到的文件的名称为" + fileName;
    }

    @RequestMapping("/r10")
    public String r10(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        StringBuilder sb = new StringBuilder();
        for (Cookie cookie : cookies) {
            sb.append(cookie.getName());
            sb.append("=");
            sb.append(cookie.getValue());
            sb.append("<br>");
        }

        return sb.toString();
    }

    @RequestMapping("/r11")
    public String r11(@CookieValue("username") String username) {
        return "username=" + username;
    }

    @RequestMapping("/setsession")
    public String setSession(HttpServletRequest request) {
        HttpSession session = request.getSession(true);
        session.setAttribute("username", "zhangsan");

        return "设置session完成";
    }

    @RequestMapping("/getsession")
    public String getSession(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        if (session != null) {
            return "username: " + (String)session.getAttribute("username");
        }

        return "session为null";
    }

    @RequestMapping("/getsession1")
    public String getSession1(HttpSession session) {
        return "username: " + (String)session.getAttribute("username");
    }

    @RequestMapping("/getsession2")
    public String getSession2(@SessionAttribute("username") String username) {
        return "username: " + username;
    }

    @RequestMapping("/getheader")
    public String getHeader(HttpServletRequest request) {
        String ua = request.getHeader("User-Agent");
        return "获取到User-Agent：" + ua;
    }

    @RequestMapping("/getheader2")
    public String getHeader1(@RequestHeader("User-Agent") String ua) {
        return "获取到User-Agent:" + ua;
    }
}
